import { commonApi } from '../api/commonApi'

export const PointerToString = (object, options) => {
  let obj = { ...object }
  options.forEach(item => {
    let [field, type = 'string'] = item.split(':')
    let val = obj[field]
    switch (type) {
      case 'string':
        obj[field] = val ? val.id : ''
        break
      case 'array':
        obj[field] = val ? val.map(v => v.id) : []
        break
    }
  })
  return obj
}

export const BuildPointer = (object, options) => {
  let obj = { ...object }
  for (let field in options) {
    let collectionName = options[field]
    let val = obj[field]
    if (val instanceof Array) {
      obj[field] = val.map(v => commonApi.createInstance(collectionName, v.id ? v.id : v))
    } else {
      obj[field] = val ? commonApi.createInstance(collectionName, val.id ? val.id : val) : null
    }
  }
  return obj
}

export const ReplaceDictionary = (results, options) => {
  let isArray = results instanceof Array
  let data = isArray ? results : [results]
  data = data.map(item => {
    let obj = { ...item }
    for (let _field in options) {
      let [field, $label = 'D_text', $value = 'D_code'] = _field.split(':')
      let val = options[_field]
      let _value = obj[field]
      if (_value instanceof Array) {
        let _a = _value.map(_item => {
          let o = val.find(v => v[$value] === _item)
          return o ? o[$label] : _item
        })
        obj[field] = _a.join(',')
      } else if (typeof _value === 'object') {
        let o = val.find(v => v[$value] === _value.id)
        obj[field] = o ? o[$label] : _value
      } else {
        let o = val.find(v => v[$value] === _value)
        obj[field] = o ? o[$label] : _value
      }
    }
    return obj
  })
  return isArray ? data : data[0]
}

// 构建树
export function BuildTree(data, parentId, pidField, idField = 'id', nodeOrder) {
  let list = []
  data.forEach((item) => {
    if (item[pidField] === parentId || (!parentId && !item[pidField])) {
      let children = BuildTree(data, item[idField], pidField, idField, nodeOrder)
      if (children.length) {
        if (nodeOrder) {
          children.sort((a, b) => {
            let _a = a[nodeOrder] || 999
            let _b = b[nodeOrder] || 999
            return Number(_a) - Number(_b)
          })
        }
        item.children = children
      }
      list.push(item)
    }
  })
  return list
}

// 从树上获取node
export function findNodeFromTree(tree, node, id = 'id', children = 'children') {
  if (!node || !node.length || !tree || !tree.length) return []
  const findItem = (item, list) => {
    for (let i = 0; i < list.length; i++) {
      if (list[i][id] === item) {
        return list[i]
      } else if (list[i][children] && list[i][children].length) {
        let res = findItem(item, list[i][children])
        if (res) {
          return res
        }
      }
    }
    return null
  }
  return node.map(item => findItem(item, tree))
}

// 从node中获取该node下所有的孩子
export function findChildrenFromTree(node, children = 'children') {
  if (!node) return []
  let results = []
  const fn = item => {
    results.push(item)
    if (item[children]) {
      item[children].forEach(v => {
        fn(v)
      })
    }
  }
  fn(node)
  return results
}

export const CanUpdate = (curVersion, reqVersion) => {
  let curVersionArr = curVersion.split('.')
  let reqVersionArr = reqVersion.split('.')

  // 将两个版本号拆成数字
  let minL = Math.min(curVersionArr.length, reqVersionArr.length)
  let pos = 0
  let diff = 0

  while (pos < minL) {
    diff = parseInt(curVersionArr[pos] - parseInt(reqVersionArr[pos]))
    if (diff !== 0) break
    pos++
  }

  if (diff > 0) {
    console.log('新版本')
  } else if (diff === 0) {
    console.log('稳定版')
  } else {
    console.log('旧版本')
    return true
  }
  return false
}
